/*
 * @Author: LibraStalker 1299640057@qq.com
 * @Date: 2023-01-27 23:44:51
 * @FilePath: new_slice_test.ts
 * @Description: 测试slice算子
 */
function getSliceIndex(
    {},
    {
        axes,
        starts,
        ends,
        decrease_axis
    },
    {
        _index_arr,
        shape
    }
) {
    let index_arr;
    if (_index_arr === undefined) {
        if (shape === undefined) { // 默认值为从0到总长度
            return;
        }
        let current_index_num = 0;
        function getIndexArrFromShape(shape) {
            const arr = [];
            if (shape.length === 1) {
                for (let i = 0; i < shape[0]; i++, current_index_num++) {
                    arr.push(current_index_num);
                }
                return arr;
            }
            for (let i = 0; i < shape[0]; i++) {
                arr.push(getIndexArrFromShape(shape.slice(1, shape.length)));
            }
            return arr;
        }
        index_arr = getIndexArrFromShape(shape);
    } else {
        index_arr = _index_arr;
    }
    // const index_arr = (typeof _index_arr) !== 'number' ? _index_arr : Array.from(Array(_index_arr).keys());
    const slicers = [];
    const if_decrease_axis = [];
    for (let i = 0; i <= Math.max(...axes.concat(decrease_axis)); i++) { // 取axes和decrease_axis中所有元素的最大值，防止漏处理之后的维度
        if (axes.includes(i)) {
            const axis_index = axes.indexOf(i);
            slicers.push([starts[axis_index], ends[axis_index]]); // 当前维度切片的起止位置
        }
        else {
            slicers.push([undefined, undefined]); // 当前维度不需要切片，直接[...].slice(undefined, undefined)就是返回原数组的复制
        }
        if_decrease_axis.push(decrease_axis.includes(i + 1)); // 这里意味着合并i到i+1这两个维度
    }
    return getSliceIndexRecursively(index_arr, slicers, if_decrease_axis);
}

function getSliceIndexRecursively(index_arr, slicers, if_decrease_axis) {
    if (slicers.length === 0) {
        return index_arr; // 如果不再需要切片了就直接返回
    }
    const [start, end] = slicers.shift(); // 当前维度需要切片的开始位置和结束位置
    if (typeof index_arr[0] === 'number') {
        return index_arr.slice(start, end); // 如果只有一个维度就直接切片
    }
    const decrease_next_axis = if_decrease_axis.shift(); // 是否将此维度与下一维度合并
    let sliced_arr = index_arr.slice(start, end).map(sub_arr => getSliceIndexRecursively(
        sub_arr, [...slicers],[...if_decrease_axis])); // 解包后重新组合成为原先的复制而不是引用，slicers每次两个一起pop出来且不会分别修改所以不用深复制
    if (decrease_next_axis) {
        sliced_arr = sliced_arr.flat(); // 合并此维度与下一维度
    }
    return sliced_arr;
}debugger;
console.log(getSliceIndex({}, {
        axes: [1],
        starts: [0],
        ends: [1],
        decrease_axis: [1,2,3]
    },
    {shape:[1,3,1,1]}));
console.log(getSliceIndex({}, {
        axes: [1, 2],
        starts: [0, 1],
        ends: [2, 3],
        decrease_axis: [1]
    },
    {shape:[2,3,4]}));
console.log(getSliceIndex({}, {
        axes: [1, 2],
        starts: [0, 1],
        ends: [2, 3],
        decrease_axis: [1]
    },
    {
        _index_arr:
            [
                [[0, 1, 2, 3],
                [4, 5, 6, 7],
                [8, 9, 10, 11]],
                [[12, 13, 14, 15],
                [16, 17, 18, 19],
                [20, 21, 22, 23]]
            ]
    }));